package org.lanqiao.lg.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.lanqiao.bean.Company;
import org.lanqiao.lg.dao.impl.CompanyDaoImpl;
import org.lanqiao.tools.BaseServlet;
import org.lanqiao.tools.ResponseData;

import com.google.gson.Gson;

@WebServlet("/CompanyServlet")
public class CompanyServlet extends BaseServlet {

	Gson gson = new Gson();
	CompanyDaoImpl comdao = new CompanyDaoImpl();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Object id = request.getSession().getAttribute("id");
		Object typeU = request.getSession().getAttribute("typeU");
		this.request = request;
		this.response = response;

		String key = getString("key");
		System.out.println(request.getSession().getId() + key + "/id=" + id + "/typeU=" + typeU);
		redata.setMsg("");
		if (!isNull("key")) {
			switch (key) {
			case "getCid":
				getCid(id);
				break;
			case "getComid":
				getComid();
				break;
			case "perfect":
				perfectCom();
				break;
			case "uplabel":
				uplabel();
				break;
			case "upInL":
				upInL();
				break;
			case "select":
				selectCom();
				break;
			case "search":
				searchPo();
				break;
			case "searchold":
				searcholdPo();
				break;
			case "searchRes2":
				searchRes2();
				break;
			case "searchRes3":
				searchRes3();
				break;
			case "searchRes4":
				searchRes4();
				break;
			default:
				redata.setCode(3);
				redata.setMsg("key参数异常");
				redata.setData("");
				break;
			}
		} else {
			redata.setCode(3);
			redata.setMsg("key参数为空");
			redata.setData("");
		}
		System.out.println(redata.toString());
		response.getWriter().write(gson.toJson(redata));
	}

// 查询该职位下被投递的简历2
	private void searchRes2() {
		Integer pid = getInt("positionId");
		System.out.println(pid);
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		List<Map<String, Object>> pifno = comdao.selectResByPId2(pid);
		if (pifno != null) {
			redata.setCode(2);
			redata.setMsg("selectResByPId2 成功");
			redata.setData(pifno);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(pifno);
			return;
		}
	}

	// 查询该职位下被投递的简历3
	private void searchRes3() {
		Integer pid = getInt("positionId");
		System.out.println(pid);
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		List<Map<String, Object>> pifno = comdao.selectResByPId3(pid);
		if (pifno != null) {
			redata.setCode(2);
			redata.setMsg("selectResByPId3 成功");
			redata.setData(pifno);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(pifno);
			return;
		}
	}

	// 查询该职位下被投递的简历4
	private void searchRes4() {
		Integer pid = getInt("positionId");
		System.out.println(pid);
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		List<Map<String, Object>> pifno = comdao.selectResByPId4(pid);
		if (pifno != null) {
			redata.setCode(2);
			redata.setMsg("selectResByPId4 成功");
			redata.setData(pifno);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(pifno);
			return;
		}
	}

	// 查询该公司下线的职位
	private void searcholdPo() {
		Integer cid = getInt("companyId");
		System.out.println(cid);
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		List<Map<String, Object>> pifno = comdao.selectOldPositionByCId(cid);
		if (pifno != null) {
			redata.setCode(2);
			redata.setMsg("selectOldPositionByCId 成功");
			redata.setData(pifno);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(pifno);
			return;
		}

	}

//查询该公司发布的职位
	private void searchPo() {
		Integer cid = getInt("companyId");
		System.out.println(cid);
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		List<Map<String, Object>> pifno = comdao.selectPositionByCId(cid);
		if (pifno != null) {
			redata.setCode(2);
			redata.setMsg("selectPositionByCId 成功");
			redata.setData(pifno);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(pifno);
			return;
		}

	}

//	查询公司信息并返回
	private void selectCom() {
		Integer id = getInt("companyId");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		Company cinfo = comdao.selectCompanyById(id);
		if (cinfo != null) {
			redata.setCode(2);
			redata.setMsg("insertCompanyLabel 成功");
			redata.setData(cinfo);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(cinfo);
			return;
		}
	}

//	更新公司详细介绍
	private void upInL() {
		Integer id = getInt("companyId");
		String introductionL = getString("introductionL");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		if ("undefined".equals(introductionL)) {
			return;
		}
		Integer i = comdao.insertCompanyInL(id, introductionL);
		if (i > 0) {
			redata.setCode(2);
			redata.setMsg("insertCompanyLabel 成功");
			redata.setData(i);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(i);
			return;
		}
	}

// 更新标签集合id
	private void uplabel() {
		Integer id = getInt("id");
		String labelId = getString("labelId");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		Integer i = comdao.insertCompanyLabel(new Company(id, labelId));
		if (i > 0) {
			redata.setCode(2);
			redata.setMsg("insertCompanyLabel 成功");
			redata.setData(i);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(i);
			return;
		}
	}

	// 返回公司id
	private void getComid() {
		Object cid = request.getSession().getAttribute("cid");
		System.out.println(cid);
		if (cid != null) {
			redata.setCode(2);
			redata.setMsg("cid已经传达");
			redata.setData(cid);
		} else {
			redata.setCode(3);
			redata.setMsg("cid异常");
			redata.setData("");
		}
	}

//返回公司id
	private void getCid(Object cid) {
		if (cid != null) {
			redata.setCode(2);
			redata.setMsg("cid已经传达");
			redata.setData(cid);
		} else {
			redata.setCode(3);
			redata.setMsg("cid异常");
			redata.setData("");
		}
	}

//	公司完善信息 状态转到2
	private void perfectCom() {
		String fullName = getString("fullName");
		String shortName = getString("shortName");
		String com = getString("com");
		String address = getString("address");
		String field = getString("field");
		String introductionS = getString("introductionS");
		Integer number = getInt("number");
		Integer period = getInt("period");
		Integer logId = getInt("logId");
		Integer id = getInt("id");
		System.out.println(id);
		Company info = new Company(id, fullName, shortName, address, com, number, field, logId, period, introductionS);
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		Integer i = comdao.insertCompanyName(info);
		if (i > 0) {
			redata.setCode(2);
			redata.setMsg("insertCompanyName 成功");
			redata.setData(i);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(i);
			return;
		}

	}

}
